The purpose of this notebook is to show how to use the Agilepy scientific API.
from agilepy.api.AGAnalysis import AGAnalysis
# Interactive plot
%matplotlib widget
confFilePath = "$HOME/agilepy_conf.yaml"
AGAnalysis.getConfiguration(
evtfile="$AGILE/agilepy-test-data/test_dataset_6.0/EVT/EVT.index",
logfile="$AGILE/agilepy-test-data/test_dataset_6.0/LOG/LOG.index",
confFilePath = confFilePath,
userName = "user-xxx",
sourceName = "vela-xxx",
tmin = 433857532,
tmax = 434289532,
timetype = "TT",
glon = 263.55,
glat = -2.78,
outputDir = "$HOME/agilepy_analysis",
verboselvl = 0
)
ag = AGAnalysis(confFilePath)
Print all options of the configuration file
ag.printOptions()
{ 'ap': {'radius': 3, 'timeslot': 3600},
'input': { 'evtfile': '/Users/bulgarelli/opt/anaconda3/envs/agilepyenv/agiletools/agilepy-test-data/test_dataset_6.0/EVT/EVT.index',
'logfile': '/Users/bulgarelli/opt/anaconda3/envs/agilepyenv/agiletools/agilepy-test-data/test_dataset_6.0/LOG/LOG.index'},
'maps': { 'binsize': 0.25,
'energybins': [[100, 10000]],
'expstep': 4.0,
'fovbinnumber': 1,
'mapsize': 40,
'offaxisangle': 30,
'proj': 'ARC',
'projtype': 'WCS',
'spectralindex': 2.1,
'timestep': 160,
'useEDPmatrixforEXP': False},
'mle': { 'contourpoints': 40,
'edpcorrection': 0.75,
'expratio_maxthr': 15,
'expratio_minthr': 0,
'expratio_size': 10,
'expratioevaluation': True,
'fluxcorrection': 0,
'integratortype': 1,
'loccl': 5.99147,
'mindefaulttolerance': 0.01,
'minimizeralg': 'Migrad',
'minimizerdefstrategy': 2,
'minimizertype': 'Minuit',
'ranal': 10,
'ulcl': 2},
'model': { 'emax_sources': 10000,
'emin_sources': 100,
'galcoeff': [-1],
'galmode': 1,
'galmode2': 0,
'galmode2fit': 0,
'isocoeff': [-1],
'isomode': 1,
'isomode2': 0,
'isomode2fit': 0,
'modelfile': None},
'output': { 'filenameprefix': 'analysis_product',
'logfilenameprefix': 'analysis_log',
'outdir': PosixPath('/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603'),
'sourcename': 'user-xxx',
'username': 'vela-xxx',
'verboselvl': 0},
'plotting': {'twocolumns': False},
'selection': { 'albedorad': 80,
'bpointing': None,
'dq': 0,
'emax': 10000,
'emin': 100,
'filtercode': 5,
'fovradmax': 60,
'fovradmin': 0,
'glat': -2.78,
'glon': 263.550001,
'lonpole': 180,
'lpointing': None,
'maplistgen': 'None',
'phasecode': 6,
'proj': 'ARC',
'timelist': 'None',
'timetype': 'TT',
'tmax': 434289532.0,
'tmin': 433857532.0}}
Print a single section of the yaml configuration file
ag.printOptions("maps")
{ 'binsize': 0.25,
'energybins': [[100, 10000]],
'expstep': 4.0,
'fovbinnumber': 1,
'mapsize': 40,
'offaxisangle': 30,
'proj': 'ARC',
'projtype': 'WCS',
'spectralindex': 2.1,
'timestep': 160,
'useEDPmatrixforEXP': False}
Print a single option
ag.getOption("energybins")
[[100, 10000]]
Set some options
ag.setOptions(energybins=[[100,300], [300, 1000]], mapsize=50, binsize=0.4)
ag.printOptions("maps")
{ 'binsize': 0.4,
'energybins': [[100, 300], [300, 1000]],
'expstep': 4.0,
'fovbinnumber': 1,
'mapsize': 50,
'offaxisangle': 30,
'proj': 'ARC',
'projtype': 'WCS',
'spectralindex': 2.1,
'timestep': 160,
'useEDPmatrixforEXP': False}
sources = ag.loadSourcesFromCatalog("2AGL", rangeDist = (0, 25))
type(sources[0])
agilepy.core.SourceModel.Source
for s in sources:
print(s)
----------------------------------------------------------- Source name: 2AGLJ1045-5954 (PointSource) * Position: - start_pos: (287.697, -0.788886) - dist from (l,b): 24.2159 * Spectrum: (PowerLaw) - flux: 1.80893e-07 - index: 2.0414 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1048-5836 (PointSource) * Position: - start_pos: (287.422, 0.423015) - dist from (l,b): 24.0778 * Spectrum: (PowerLaw) - flux: 1.54772e-07 - index: 2.00039 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1045-5735 (PointSource) * Position: - start_pos: (286.672, 1.23211) - dist from (l,b): 23.4607 * Spectrum: (PowerLaw) - flux: 1.64949e-07 - index: 2.25973 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1059-5233 (PointSource) * Position: - start_pos: (286.128, 6.61213) - dist from (l,b): 24.4183 * Spectrum: (PowerLaw) - flux: 3.14104e-07 - index: 1.77974 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1029-5834 (PointSource) * Position: - start_pos: (285.221, -0.689424) - dist from (l,b): 21.7604 * Spectrum: (PowerLaw) - flux: 2.64853e-07 - index: 1.91101 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1015-5852 (PointSource) * Position: - start_pos: (283.945, -1.85807) - dist from (l,b): 20.3987 * Spectrum: (PowerLaw) - flux: 1.91737e-07 - index: 2.03933 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1020-5752 (PointSource) * Position: - start_pos: (283.95, -0.621559) - dist from (l,b): 20.5036 * Spectrum: (PowerLaw) - flux: 2.72475e-07 - index: 2.12932 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1020-5906 (PointSource) * Position: - start_pos: (284.629, -1.89938) - dist from (l,b): 21.0794 * Spectrum: (PowerLaw) - flux: 1.38792e-07 - index: 1.78216 * Free params: none -----------------------------------------------------------
The sources that are already present in the Sources Library will not be loaded twice.
sources_hypotesis = """
969.539e-08 263.585 -2.84083 1.71345 0 2 2AGLJ0835-4514 0.0 2 3913.06 1.34774 0.5 5 20 10000 0 100
10e-08 273.0 -1.1 2.1 1 2 NEWOURCE 0.0 0 0 0 0.5 5 20 10000 0 100
"""
with open("./sources_hyp.txt", "w") as sf:
sf.write(sources_hypotesis)
#sources = ag.loadSourcesFromFile("./sources_hyp.txt", rangeDist = (0, 25))
The selection criteria can be expressed using the following Source class’s parameters:
selectedSources = ag.selectSources('flux > 0')
len(selectedSources)
9
selectedSources = ag.selectSources('name == "2AGLJ0835-4514"')
len(selectedSources)
for s in selectedSources:
print(s)
----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: none -----------------------------------------------------------
selectedSources = ag.selectSources("flux > 0 AND dist <= 2")
len(selectedSources)
for s in selectedSources:
print(s)
----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: none -----------------------------------------------------------
You can fix or free the following parametes:
_sources = ag.freeSources('name == "2AGLJ0835-4514"', "flux", True, show=True)
for s in _sources:
print(s)
----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: flux -----------------------------------------------------------
_sources = ag.freeSources('name == "2AGLJ0835-4514"', "index1", True)
for s in _sources:
print(s)
----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: flux index1 -----------------------------------------------------------
_sources = ag.freeSources("flux > 0 AND dist <= 2", "flux", True)
#len is 0 because the flux is already free
len(_sources)
0
for s in _sources:
print(s)
Resetting the changes:
_sources = ag.freeSources('name == "2AGLJ0835-4514"', "flux", False)
ag.freeSources('name == "2AGLJ0835-4514"', "index1", False)
[<agilepy.core.SourceModel.Source at 0x7ffae8a7f1d0>]
ag.freeSources("flux > 0 AND dist <= 2", "flux", False)
[]
deleted = ag.deleteSources('name == "2AGLJ1048-5836"').pop()
print(deleted)
----------------------------------------------------------- Source name: 2AGLJ1048-5836 (PointSource) * Position: - start_pos: (287.422, 0.423015) - dist from (l,b): 24.0778 * Spectrum: (PowerLaw) - flux: 1.54772e-07 - index: 2.00039 * Free params: none -----------------------------------------------------------
len(ag.getSources())
8
Passing a dictionary:
newSourceDict = {
"glon" : 273.0,
"glat": -1.1,
"spectrumType" : "PowerLaw",
"flux": 10e-08,
"index": 2.1
}
if ag.addSource("NEWSOURCE", newSourceDict):
print("Source loaded")
Source loaded
len(ag.getSources())
9
Passing a Source object:
type(deleted)
agilepy.core.SourceModel.Source
if ag.addSource("NEWSOURCE", deleted):
print("Source loaded")
else:
print("Source is already present in the SourcesLibrary")
2021-01-16 09:26:15,618 [WARNING ] [SourcesLibrary] The source NEWSOURCE already exists. The 'sourceObject' will not be added to the SourcesLibrary. Source is already present in the SourcesLibrary
deletedSources = ag.deleteSources('name == "NEWSOURCE"')
len(ag.getSources())
8
sources = ag.selectSources('name == "2AGLJ0835-4514"')
source = sources.pop()
source.spectrum.set("index2", 1.34774)
print(source)
----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: none -----------------------------------------------------------
This method returns the selected sources affected by the change
sources = ag.freeSources('name == "2AGLJ0835-4514"', "flux", True, show=True)
Final source hypothesis for the analysis
selectedSources = ag.selectSources('flux > 0')
print(len(selectedSources))
for s in selectedSources:
print(s)
8 ----------------------------------------------------------- Source name: 2AGLJ1045-5954 (PointSource) * Position: - start_pos: (287.697, -0.788886) - dist from (l,b): 24.2159 * Spectrum: (PowerLaw) - flux: 1.80893e-07 - index: 2.0414 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1045-5735 (PointSource) * Position: - start_pos: (286.672, 1.23211) - dist from (l,b): 23.4607 * Spectrum: (PowerLaw) - flux: 1.64949e-07 - index: 2.25973 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1059-5233 (PointSource) * Position: - start_pos: (286.128, 6.61213) - dist from (l,b): 24.4183 * Spectrum: (PowerLaw) - flux: 3.14104e-07 - index: 1.77974 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1029-5834 (PointSource) * Position: - start_pos: (285.221, -0.689424) - dist from (l,b): 21.7604 * Spectrum: (PowerLaw) - flux: 2.64853e-07 - index: 1.91101 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1015-5852 (PointSource) * Position: - start_pos: (283.945, -1.85807) - dist from (l,b): 20.3987 * Spectrum: (PowerLaw) - flux: 1.91737e-07 - index: 2.03933 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1020-5752 (PointSource) * Position: - start_pos: (283.95, -0.621559) - dist from (l,b): 20.5036 * Spectrum: (PowerLaw) - flux: 2.72475e-07 - index: 2.12932 * Free params: none ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.69539e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: flux ----------------------------------------------------------- ----------------------------------------------------------- Source name: 2AGLJ1020-5906 (PointSource) * Position: - start_pos: (284.629, -1.89938) - dist from (l,b): 21.0794 * Spectrum: (PowerLaw) - flux: 1.38792e-07 - index: 1.78216 * Free params: none -----------------------------------------------------------
ag.printOptions("maps")
{ 'binsize': 0.4,
'energybins': [[100, 300], [300, 1000]],
'expstep': 4.0,
'fovbinnumber': 1,
'mapsize': 50,
'offaxisangle': 30,
'proj': 'ARC',
'projtype': 'WCS',
'spectralindex': 2.1,
'timestep': 160,
'useEDPmatrixforEXP': False}
maplistfile = ag.generateMaps()
ag.displayCtsSkyMaps(smooth=2, regFiles=["$AGILE/catalogs/3EG_1.reg"], regFileColors=["red"], catalogRegions="2AGL", catalogRegionsColor="blue", normType="sqrt")
[None]
ag.displayExpSkyMaps(singleMode = False)
[None, None]
ag.displayGasSkyMaps()
[None]
ag.displayCtsSkyMaps(smooth=3, saveImage=True)
['/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/plots/analysis_cts_20210116-092700.png']
print("isocoeff: {} galcoeff: {}".format(ag.getOption("isocoeff"), ag.getOption("galcoeff")))
isocoeff: [-1, -1] galcoeff: [-1, -1]
isoBkg, galBkg, maplistfile = ag.calcBkg("2AGLJ0835-4514", galcoeff = [0.7, 0.7], pastTimeWindow = 0)
# calcBkg with pastTimeWindow = 14
# ag.setOptions(galcoeff=[0.469176, 0.699403, 0.199108, 0.533379], isocoeff=[4.27314, 0.976644, 8.12386, 1.24627])
print("isocoeff: {} galcoeff: {}".format(ag.getOption("isocoeff"), ag.getOption("galcoeff")))
isocoeff: [4.08416, 3.84041] galcoeff: [0.7, 0.7]
sourceFiles = ag.mle()
#print results
sources = ag.selectSources("sqrtTS > 0")
print(len(sources))
for s in sources:
print(s)
1 ----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) => sqrt(ts): 17.008 * Position: - start_pos: (263.585, -2.84083) - dist from (l,b): 0.0702 * Spectrum: (PLSuperExpCutoff) - flux: 9.01664e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: flux * Multi analysis: - flux(ph/cm2s): 9.01664e-06 +- 9.19068e-07 - upper limit(ph/cm2s): 1.0951e-05 - ergLog(erg/cm2s): 1.60296e-09 +- 1.6339e-10 - galCoeff: [0.7, 0.7] - isoCoeff: [4.08416, 3.84041] - exposure(cm2s): 18945200.0 - exp-ratio: 0.0 -----------------------------------------------------------
Free also position parameter
ag.freeSources('name == "2AGLJ0835-4514"', "pos", True, show=True)
[<agilepy.core.SourceModel.Source at 0x7ffb08917d90>]
ag.mle()
['/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1045-5954.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1045-5735.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1059-5233.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1029-5834.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1015-5852.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1020-5752.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ0835-4514.source', '/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/mle/analysis_product0000_2AGLJ1020-5906.source']
#print results
sources = ag.selectSources("sqrtTS > 0")
print(len(sources))
for s in sources:
print(s)
1 ----------------------------------------------------------- Source name: 2AGLJ0835-4514 (PointSource) => sqrt(ts): 17.0229 * Position: - start_pos: (263.647, -2.8547) - dist from (l,b): 0.1223 * Spectrum: (PLSuperExpCutoff) - flux: 8.98066e-06 - index1: 1.71345 - cutoffEnergy: 3913.06 - index2: 1.34774 * Free params: flux pos * Multi analysis: - flux(ph/cm2s): 8.98066e-06 +- 9.15265e-07 - upper limit(ph/cm2s): 1.09073e-05 - ergLog(erg/cm2s): 1.59656e-09 +- 1.62714e-10 - galCoeff: [0.7, 0.7] - isoCoeff: [4.08416, 3.84041] - exposure(cm2s): 18945200.0 - exp-ratio: 0.0 - L_peak: 263.638 - B_peak: -2.85605 - distFromStartPos: 0.0548425 - ellipse: - L: 263.647 - B: -2.8547 - distFromStartPos: 0.0637012 - r: 0.199311 - a: 0.196811 - b: 0.202633 - phi: 42.9398 -----------------------------------------------------------
Save the results of the analysis in a reg file.
regFile = ag.writeSourcesOnFile("source_regions", "reg")
print(regFile)
/Users/bulgarelli/agilepy_analysis/vela-xxx_user-xxx_20210116-092603/sources_library/source_regions.reg
Display the result of the analysis saved in a reg file.
ag.displayCtsSkyMaps(smooth=2, regFiles=[regFile], regFileColors=["red"], catalogRegions="2AGL", catalogRegionsColor="blue")
[None]
Fix the position and keep only flux free
ag.freeSources('name == "2AGLJ0835-4514"', "pos", False, show=True)
[<agilepy.core.SourceModel.Source at 0x7ffb08917d90>]
lightCurveData = ag.lightCurveMLE("2AGLJ0835-4514", binsize=86400)
! cat $lightCurveData
time_start_mjd time_end_mjd sqrt(ts) flux flux_err flux_ul gal iso l_peak b_peak dist l b r ell_dist time_start_utc time_end_utc time_start_tt time_end_tt 58026.49921296296 58027.49921296296 7.34802 944.077e-08 213.086e-08 1418.53e-08 0.7,0.7 4.08416,3.84041 263.647 -2.8547 0.0 -1.0 -1.0 -1.0 -1.0 2017-09-30T11:58:52 2017-10-01T11:58:52 433857532.0 433943932.0 58027.49921296296 58028.49921296296 8.88107 1054.87e-08 211.633e-08 1523.64e-08 0.7,0.7 4.08416,3.84041 263.647 -2.8547 0.0 -1.0 -1.0 -1.0 -1.0 2017-10-01T11:58:52 2017-10-02T11:58:52 433943932.0 434030332.0 58028.49921296296 58029.49921296296 7.31826 820.89e-08 198.193e-08 1266.73e-08 0.7,0.7 4.08416,3.84041 263.647 -2.8547 0.0 -1.0 -1.0 -1.0 -1.0 2017-10-02T11:58:52 2017-10-03T11:58:52 434030332.0 434116732.0 58029.49921296296 58030.49921296296 6.7938 840.137e-08 208.073e-08 1306.49e-08 0.7,0.7 4.08416,3.84041 263.647 -2.8547 0.0 -1.0 -1.0 -1.0 -1.0 2017-10-03T11:58:52 2017-10-04T11:58:52 434116732.0 434203132.0 58030.49921296296 58031.49921296296 7.62835 820.045e-08 190.836e-08 1249.26e-08 0.7,0.7 4.08416,3.84041 263.647 -2.8547 0.0 -1.0 -1.0 -1.0 -1.0 2017-10-04T11:58:52 2017-10-05T11:58:52 434203132.0 434289532.0
ag.displayLightCurve("mle")
ap_file, _ = ag.aperturePhotometry()
! cat $ap_file
ag.displayLightCurve("ap")
ag.deleteAnalysisDir()